﻿using System.Collections.ObjectModel;
using System.Windows.Controls;
using System.Windows.Input;
using AvalonDock;
using Notoric.ViewModel;
using Notoric.ViewModel.Elements;
using System.Windows;

namespace Notoric.Gui.Windows
{
    /// <summary>
    /// Interaction logic for ProjectExplorer.xaml
    /// </summary>
    public partial class ProjectExplorer : DockableContent
    {
        #region Constructors

        /// <summary>
        /// Creates a new <see cref="ProjectExplorer"/> instance.
        /// </summary>
        public ProjectExplorer()
        {
            projects = new ObservableCollection<Project>();

            InitializeComponent();
        }

        #endregion

        #region Event Handlers

        /// <summary>
        /// Occurs when the user presses a key in the project tree.
        /// </summary>
        /// <param name="sender">Object where the event handler is attached</param>
        /// <param name="e">Event data</param>
        private void projectTree_KeyDown(object sender, KeyEventArgs e)
        {
            var selectedItem = projectTree.SelectedItem as IProjectExplorerItem;
            if (selectedItem == null)
                return;

            if (e.Key == Key.F2)
            {
                ProjectExplorerCommands.RenameCommand.Execute(selectedItem, this);
                e.Handled = true;
            }
            else if (e.Key == Key.Enter)
            {
                ProjectExplorerCommands.OpenCommand.Execute(selectedItem, this);
                e.Handled = true;
            }
            else if (e.Key == Key.Delete)
            {
                ProjectExplorerCommands.DeleteCommand.Execute(selectedItem, this);
                e.Handled = true;
            }
        }

        /// <summary>
        /// Occurs when the user double clicks a tree view item.
        /// </summary>
        /// <param name="sender">Object where the event handler is attached</param>
        /// <param name="e">Event data</param>
        private void projectTree_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (projectTree.SelectedItem != null && projectTree.SelectedItem is IProjectExplorerItem)
                ProjectExplorerCommands.OpenCommand.Execute(projectTree.SelectedItem, this);
        }

        /// <summary>
        /// Occurs when the user presses the right mouse button on a tree view item.
        /// </summary>
        /// <param name="sender">Object where the event handler is attached</param>
        /// <param name="e">Event data</param>
        private void TreeViewItem_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            TreeViewItem item = sender as TreeViewItem;
            if (item != null)
                item.IsSelected = true;
            e.Handled = true;
        }

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets the collection of projects currently open in the project explorer.
        /// </summary>
        public ObservableCollection<Project> Projects
        {
            get { return projects; }
        }

        #endregion

        #region Private Fields

        /// <summary>
        /// Holds the collection of open projects.
        /// </summary>
        private ObservableCollection<Project> projects;

        #endregion
    }
}
